﻿using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

using Outcoder;
using Outcoder.Services;
using Outcoder.UI.Xaml;

namespace CalciumTemplateApp
{
	public sealed class BindableResources : INotifyPropertyChanged
	{
		static readonly BindableChangeNotifier<AppResources> resources
			= new BindableChangeNotifier<AppResources>(new AppResources());

		static BindableResources instance;

		public BindableResources()
		{
			if (!EnvironmentValues.DesignTime && instance != null)
			{
				throw new InvalidOperationException(
					"Only one instance of this class can be created.");
			}

			instance = this;
		}

		public static BindableChangeNotifier<AppResources> Resources
		{
			get
			{
				return resources;
			}
		}

		public static void RaiseNotifyPropertyChanged()
		{
			resources.RaisePropertyChanged();
			if (instance != null)
			{
				instance.OnPropertyChanged(string.Empty);
			}
		}

		public object this[string key]
		{
			get
			{
				var stringParserService = Dependency.Resolve<IStringParserService>();
				return stringParserService.Parse(key);
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		void OnPropertyChanged([CallerMemberName] string propertyName = null)
		{
			PropertyChangedEventHandler handler = PropertyChanged;
			if (handler != null)
			{
				handler(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}
